#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "MyString.h"
#include "DynamicArray.h"
#include "Tool.h"
#define Map_Size 6
#define true 1
#define false 0


int main()
{
    srand(time(NULL));
    system("clear");
    printf("*************************************************************************\n");
    printf("*   	                   欢迎来到魔塔世界！(^=^)!                     *\n");
    printf("*************************************************************************\n");

    char board[Map_Size][Map_Size];
    InitBoard(board);

    struct Player player;
    InitPlayer(&player);

    struct DynamicArray monsters = {NULL, 10, 0};
    InitMonsters(&monsters);

    struct DynamicArray treasures = {NULL, 10, 0};
    InitTreasure(&treasures);

    struct DynamicArray coins = {NULL, 10, 0};
    InitCoin(&coins);

    struct DynamicArray npcas = {NULL, 1, 0};
    InitNonPlayerA(&npcas);

    struct DynamicArray npcbs = {NULL,1,0};
    InitNonPlayerB(&npcbs);

    struct DynamicArray npccs = {NULL,1,0};
    InitNonPlayerC(&npccs);

    while (1)
    {
        // PrintMap(board, &player);
        printf("你当前所在的位置是<%d , %d>\n ", player.x + 1, player.y + 1);

        for (int i = 0; i < monsters.len; i++)
        {
            struct Monster *monster = (struct Monster *)monsters.dp[i];
            if (monster->HP > 0 && monster->x == player.x && monster->y == player.y)
            {
                if (Battle(&player, monster) == false)
                {
                    printf("Game over!\n");
                    break;
                }
            }
        }

        for (int i = 0; i < treasures.len; i++)
        {
            struct Treasure *treasure = (struct Treasure *)treasures.dp[i];
            if (treasure->ap > 0 && treasure->x == player.x && treasure->y == player.y)
            {
                if (Gain(&player, treasure) == true)
                {
                    printf("Gain over!\n");
                    break;
                }
            }
        }

        for (int i = 0; i < coins.len; i++)
        {
            struct Coin *coin = (struct Coin *)coins.dp[i];
            if (coin->value > 0 && coin->x == player.x && coin->y == player.y)
            {
                if (Money(&player, coin) == true)
                {
                    printf("Get Money\n");
                    break;
                }
            }
        }
        //NPC1
        for (int i = 0; i < npcas.len; i++)
        {
            struct NonPlayerA *npca = (struct NonPlayerA *)npcas.dp[i];
            if (npca->value > 0 && npca->x == player.x && npca->y == player.y)
            {
                if (MissA(&player, npca) == true)
                {
                    printf("Miss NPC\n");
                    break;
                }
            }
        }
        //NPC2
        for (int i = 0; i < npcbs.len; i++)
        {
            struct NonPlayerB *npcb = (struct NonPlayerB *)npcbs.dp[i];
            if (npcb->value > 0 && npcb->x == player.x && npcb->y == player.y)
            {
                if (MissB(&player, npcb) == true)
                {
                    printf("Miss NPC\n");
                    break;
                }
            }
        }
        //NPC3
        for (int i = 0; i < npccs.len; i++)
        {
            struct NonPlayerC *npcc = (struct NonPlayerC *)npccs.dp[i];
            if (npcc->value > 0 && npcc->x == player.x && npcc->y == player.y)
            {
                if (MissC(&player, npcc) == true)
                {
                    printf("Miss NPC\n");
                    break;
                }
            }
        }

        if (player.HP == 0)
            break;
        
        PrintMap(board, &player);
        printf("请选择你要进行的移动(输入'w ' , 'a' , 's ' ,'d ')or操作('f'查看状态,'o'幸运转盘) 按q退出");
        printf("\n");
        char choice;
        scanf("%c", &choice);
        if(choice == 'q')
        {
            break;
        }
        int c;
        while ((c = getchar()) != '\n' && c != EOF)
        {
        }
        system("clear");
        MakeMove(&player, choice);
        
    }

    for (int i = 0; i < treasures.len; i++)
    {
        struct Monster *monster = (struct Monster *)monsters.dp[i];
        free(monster->name.string);
        free(monster);
    }
    for (int i = 0; i < monsters.len; i++)
    {
        struct Treasure *treasure = (struct Treasure *)treasures.dp[i];
        free(treasure->name.string);
        free(treasure);
    }
    for (int i = 0; i < coins.len; i++)
    {
        struct Coin *coin = (struct Coin *)coins.dp[i];
        free(coin->name.string);
        free(coin);
    }
    //1
    for (int i = 0; i < npcas.len; i++)
    {
        struct NonPlayerA *npca = (struct NonPlayerA *)npcas.dp[i];
        free(npca->nameA.string);
        free(npca);
    }
    //2
    for (int i = 0; i < npcbs.len; i++)
    {
        struct NonPlayerB *npcb = (struct NonPlayerB *)npcbs.dp[i];
        free(npcb->nameB.string);
        free(npcb);
    }
    //3
    for (int i = 0; i < npccs.len; i++)
    {
        struct NonPlayerC *npcc = (struct NonPlayerC *)npccs.dp[i];
        free(npcc->nameC.string);
        free(npcc);
    }


    free(npcas.dp);
    free(npcbs.dp);
    free(npccs.dp);
    free(monsters.dp);
    free(treasures.dp);
    free(coins.dp);
    return 0;
}
